/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.HashMap;
import java.net.*;
import java.io.*;
import java.lang.String.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
 *
 * @author manish
 */
public class Driver extends Thread{
    private String inputString;    
    ServerSocket theServer = null;
    Socket communicator = null;
    BufferedReader in;
    PrintWriter out;
    Gui0 myGui0;
    Gui1 myGui1;

public Driver(Gui0 myGui0, Gui1 myGui1){
    this.myGui0=myGui0;
    this.myGui1=myGui1;
}
    
public void run(){
    try{
            theServer = new ServerSocket(Integer.parseInt(myGui0.getthreadServerName()));
            System.out.println("Socket set up");
            boolean finished = false;
              while (!finished) {
                  communicator = theServer.accept();
                  System.out.println("connection received");
                  in = new BufferedReader(new InputStreamReader(communicator.getInputStream()));
                  out = new PrintWriter(communicator.getOutputStream(), true);
                  inputString = in.readLine();
                  System.out.println("message: " + inputString);
                  String [] temp=null;
                  temp=inputString.split(",");
                  if (temp==null){
                      System.out.println("Message contains nothing");
                      
                  }
                  else{
                      if(temp[0].equalsIgnoreCase("Message")){
                          String BuddyId= temp[0];
                          String msg= temp[1];
                          myGui1.passMessage(BuddyId,msg);
                      }
                      else if(temp[0].equalsIgnoreCase("checkOnline")){
                          myGui1.updateOnlineHash(temp[1]);
                      }
                      else{
                          JOptionPane.showMessageDialog(new javax.swing.JFrame(), "Some Unknown Packet recieved");
                      }
                  }
                  
             }
        }
        catch (IOException e)
        {
                System.out.println("Error occurred in communication in Driver");
                System.exit(1);
        }
}
/*
public static void main(String[] args) {
    Driver server=new Driver();
        try{
            ServerSocket theServer = new ServerSocket(2000);
            System.out.println("Socket set up");
            boolean finished = false;
              while (!finished) {
                  server.communicator = theServer.accept();
                  System.out.println("connection received");
                  server.in = new BufferedReader(new InputStreamReader(server.communicator.getInputStream()));
                  server.out = new PrintWriter(server.communicator.getOutputStream(), true);
                  server.inputString = server.in.readLine();
                  System.out.println("message: " + server.inputString);
                  String [] temp=null;
                  temp=(String[]) server.breakString(server.inputString);
                  server.threadStart(temp);
              }
        }
        catch (IOException e)
        {
                System.out.println("Error occurred in communication");
                System.exit(1);
        }
    
}
public Object breakString(String s){
        String [] temp=null;
        temp=s.split(",");
        return temp;
}
*/

}
